package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private BaseAttributeMapper baseAttributeMapper;


    @Override
    public List<JSONObject> getBaseCategoryList() {
        // 返回结果
        List<JSONObject> resultJSON = new ArrayList<>();

        // 查询出所有的信息，再进行组装
        List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectBaseCategoryList();

        // 按照c1的id进行组合，Map： key：c1Id   value：符合c1的list集合
        Map<Long, List<BaseCategoryView>> category1Map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));


        // 遍历map，对c1JSON进行封装
        for (Map.Entry<Long, List<BaseCategoryView>> c1entry : category1Map.entrySet()) {
            JSONObject c1JSON = new JSONObject();
            // 封装返回属性
            c1JSON.put("categoryId", c1entry.getKey());
            c1JSON.put("categoryName", c1entry.getValue().get(0).getCategory1Name());

            // 需要循坏获取c2JSON
            List<JSONObject> c2List = new ArrayList<>();
            // 同上，根据c2的id进行分组，然后封装数据
            Map<Long, List<BaseCategoryView>> Category2Map = c1entry.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            for (Map.Entry<Long, List<BaseCategoryView>> c2entry : Category2Map.entrySet()) {
                // 封装c2json
                JSONObject c2JSON = new JSONObject();
                c2JSON.put("categoryId", c2entry.getKey());
                c2JSON.put("categoryName", c2entry.getValue().get(0).getCategory2Name());

                // 封装c3并添加
                List<JSONObject> c3List = new ArrayList<>();
                List<BaseCategoryView> c3ViewList = c2entry.getValue();
                for (BaseCategoryView baseCategoryView : c3ViewList) {
                    JSONObject c3JSON = new JSONObject();
                    c3JSON.put("categoryId", baseCategoryView.getCategory3Id());
                    c3JSON.put("categoryName", baseCategoryView.getCategory3Name());
                    c3List.add(c3JSON);
                }
                c2JSON.put("categoryChild", c3List);
                // 添加到list中
                c2List.add(c2JSON);
            }

            c1JSON.put("categoryChild", c2List);
            // 将结果添加到返回中
            resultJSON.add(c1JSON);
        }

        return resultJSON;
    }

    @Override
    public List<BaseAttribute> findAttribute(Long category1Id) {

        // 多表查询通过操作表，或者返回值来判断应该放在哪个mapper中
        return baseAttributeMapper.selectAttribute(category1Id);
    }

}
